package com.javaee6.votinator;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
@ManagedBean
public class VotePM implements Serializable {

	private Long id;
	private String fName;
	private String lName;
	private String birthDate;
	private String age;
	private String school;
	private String university;
	private String email;
	private String pw;
	
	private User user;
	@EJB
	private UserService service;

	@PostConstruct
	public void init() {

	}

	public void greet() {
		System.out.println("test");
	}

	public String login() throws NoSuchUserException {
		id = service.login(this.email, this.pw);
		System.out.println("NO IM SO PRO");
		
		if(id!=0){
			id-=1; 
			System.out.println(id);
			user = service.find(id);
			System.out.println(user.getFirstName());
			this.fName = user.getFirstName();
			this.lName = user.getLastName();
			return "success";
		} else {
			return "failed";
		}
		
	}

	public void addUser() {
		User user = new User();
		user.setFirstName(fName);
		user.setLastName(lName);
		user.setEmail(email);
		user.setPw(pw);
		service.create(user);
	}

	// --- Getters && Setters ---

	public String getBirthDate() {
		return birthDate;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public String getUniversity() {
		return university;
	}

	public void setUniversity(String university) {
		this.university = university;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

}
